<?php
/** 
 * 求职工作
 * Author:zhang
 *
 */

namespace Api\Controller;

class WorkController extends ApiController{
	public function __construct(){
    	parent::__construct();
    	/** 测试不判断登录 modify by zhang 2016 start  **/
    	if (!I('mytest')) {
	        !api_login() && response(-94);
	        !passed_login() && response(-95);
	        !only_login() && response(-96);
    	}
    	/** 测试不判断登录 modify by zhang 2016 end **/
	}

    //企业信息
    function companyAdd(){
        $model_company = M('company');
        $uid = API_UID;
        $is_add = true;
        $data = $model_company->create();
        if (isset($data['id']) && intval($data['id'] > 0)) {
            $check = $model_company->find($data['id']);
            if ($check) {
                $is_add = false;
            }
        }
        if ($is_add) {
            $is_exist = $model_company->where('uid='.$uid)->find();
            if ($is_exist) {
                response(-2, '', '该公司已存在');
            }
            $data['uid'] = $uid;
            $id = $model_company->add($data);
        }else{
            $data['type'] = 0;
            $data['uid'] = $uid;
            $id = $model_company->save($data);
        }
        $id !== false ? response(1,array('id'=>$id)):response(-9);
    }
    /**
     * 我的公司详情
     */
    public function myCompanyDetail() {
        $uid = API_UID;
        $result = M('company')->where('uid='.$uid)->find();
        if($result) {
            $result['license'] = M('picture')->where('id=' . $result['license'])->getField('path');
        }
        $result?response(1, $result):response(-9);
    }
    /**
     * 删除我发布的工作
     */
    public function deleteMyWork() {
        $id = I('id');
        $result = M('work')->where('id='.$id)->delete();
        $result?response(1):response(-9);
    }
    /**
     * 编辑工作
     */
    public function edit() {
        $id = I('id');
        $workModel = M('work');
        $data = $workModel->create();
        $data['time'] = NOW_TIME;
        $result = $workModel->where('id='.$id)->save($data);
        $result?response(1,$result):response(-9);
    }


    //添加工作
    function add(){
        $uid = API_UID;
        $amount = M('member')->where('uid='.$uid)->getField('amount');
        if (!$amount) {
            $amount = 0;
        }
        if ($amount<2) {
            response(-2, '', '余额不足');
        }
        M('member')->where('uid='.$uid)->setDec('amount', '2');
        $workModel = M('work');
        $data = $workModel->create();
        $data['company'] = M('company')->where('id='.$data['cid'])->getField('company');
        $data['time'] = NOW_TIME;
        $result = $workModel->add($data);
        $result?response(1,$result):response(-9);
    }


	//找工作首页
	function index(){
		$word = I("post.work",'');
		$region = I("post.workplace",'');
		$treatment = I("post.treatment",'');
		$where = array();
		//按职位搜索
		if ($word) {
			$where['work|worktype'] = array('like',"%".$word."%");

        }
		//按地区搜索
		if ($region) {
			$where['workplace'] = array('like',"%".$region."%");
		}
		//按待遇搜索
		if ($treatment) {
			$where['treatment'] = array('like',"%".$treatment."%");
		}
		$where['status'] = 1;
		$data = M('work')->alias('w')
            ->field("w.id,w.cid,w.work,c.company,w.time,w.treatment,w.workplace")
            ->join("left join jzb_company c on w.cid=c.id")->where($where)->select();
		foreach ($data as $key=>$val) {
		    $data[$key]['time']  = $this->transfer_time($val['time']);
        }
		response(1,$data);
	}
    function transfer_time($time)
    {
        //获取今天凌晨的时间戳
        $day = strtotime(date('Y-m-d',time()));
        //获取昨天凌晨的时间戳
        $pday = strtotime(date('Y-m-d',strtotime('-1 day')));
        if($time<$pday){
            $str = date('Y-m-d H:i:s',$time);
        }elseif($time<$day && $time>$pday){
            $str = "昨天";
        }else{
            $str = "今天";
        }
        return $str;
    }

	//我发布的职位
	function myword(){
	    $uid = API_UID;
        $data = M('work')->alias('w')
            ->field("w.*,c.company")
            ->join("left join jzb_company c on w.cid=c.id")->where('c.uid='.$uid)->select();
		$data?response(1,$data):response(-9);
	}
    /**
     * 公司详情
     */
    public function companyDetail() {
        $id = I('id');
        $result = M('company')->where('id='.$id)->find();
        $result?response(1, $result):response(-9);
    }
    /**
     *获取岗位详情
     */
    public function getWorkDetail() {
        $id = I('id');
        $result = M('work')->alias('w')
            ->field('w.*,c.company,c.tel as ctel,c.phone,c.name,c.site,c.info')
            ->join('left join jzb_company c on w.cid=c.id')
            ->where('w.id='.$id)->find();
        if ($result) {
            $result['time'] = $this->transfer_time($result['time']);
            response(1,$result);
        } else {
            response(-9);
        }
    }

	//获取热门工作等选项
    public function getList() {
        $types = M('work_type')->select();
        $result = array();
        foreach ($types as $key=>$val) {
            $result[$key]['name'] = $val['type'];
            $result[$key]['list'] = M('work_name')->where('tid='.$val['id'])->select();
        }
        $result?response(1, $result):response(-9);
    }
}